{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rows=[]\n",
    "\n",
    "csv_number=0\n",
    "\n",
    "\n",
    "\n",
    "k_dict = {}\n",
    "k_dict_total = {}\n",
    "\n",
    "str1=i.split('_')[0]#value\n",
    "str2=i.split('_')[1]#name\n",
    "str3=i.split('_')[2]#说明书上的名字\n",
    "str4=i.split('_')[3]#说明书上的群系编号\n",
    "#csv名称\n",
    "str8=str4+ '_' +str3+'_各类型信息统计'\n",
    "str9=str4+ '_' +str3+'_分段拟合_各类型信息统计'\n",
    "str6=0\n",
    "#str6='椴、槭林'#输出二级分类的csv名\n",
    "#         csvpath=r'F:\\2csv\\6121'#运行csv所在的文件夹，该程序运行的是该文件夹下的所有的csv文件\n",
    "\n",
    "L,name=file_name(csvpath)\n",
    "path=save_path+r'\\总体拟合（有序图）'\n",
    "\n",
    "if not os.path.exists(path):\n",
    "    os.mkdir(path)\n",
    "if not os.path.exists(save_path+r'\\有序图'):\n",
    "    os.mkdir(save_path+r'\\有序图')\n",
    "\n",
    "i = List\n",
    "for str5 in name:\n",
    "    L=[]\n",
    "    print('\\n当前csv:'+str5)\n",
    "    if str5[:-4] == str8 or str5[:-4]==str9 :\n",
    "        continue\n",
    "\n",
    "####################植被小区的名字，改改改#################\n",
    "    str5_1=str5.split('.')[1]#大地域编号\n",
    "\n",
    "    if str5_1 not in type_dict and type_dict:\n",
    "        k_dict[str5] = {}\n",
    "        k_dict_total[str5] = {}\n",
    "        continue\n",
    "#########################################################\n",
    "\n",
    "    str5_2_1=str5.split('.')[0]#温度带\n",
    "    str5_3=str5.split('.')[2]#地域\n",
    "    str5_4=str5.split('.')[3].split('_')[0]#子类型\n",
    "    if  type_dict:\n",
    "        if str5_4 not in type_dict[str5_1] and  type_dict[str5_1]:\n",
    "            k_dict[str5] = {}\n",
    "            k_dict_total[str5] = {}\n",
    "            continue    \n",
    "    str5_1='地域'+str5_1\n",
    "    temprList = ['温度交错带','高山极地带', '苔原带', '寒温带', '中温带', '暖温带', '亚热带', '热带','s22','s22']\n",
    "\n",
    "    if str5_4 == '1123':\n",
    "        if str5_1=='':\n",
    "            str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]#输出图片名\n",
    "        else:\n",
    "            str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+str5_1#输出图片名\n",
    "    else:\n",
    "        if str5_1=='':\n",
    "            if str5_3=='0':\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'_'+'类型'+str5_4#输出图片名\n",
    "            else:\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "        else:\n",
    "            if str5_3=='0':\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'_'+'类型'+str5_4#输出图片名\n",
    "            else:\n",
    "                str7=str4+ '_' +str3+'_'+temprList[int(str5_2_1)]+ '_'+str5_1+'-'+str5_3+'_'+'类型'+str5_4#输出图片名\n",
    "\n",
    "    tmix_list=[]\n",
    "    tmix_str=''\n",
    "    if str5_2_1 == '0':\n",
    "        tmix=str5.split('_')[1][:-4]\n",
    "        tmix_list=tmix.split('.')\n",
    "        for TL in range(len(tmix_list)):\n",
    "            tmix_list[TL]  = temprList[int(tmix_list[TL])]\n",
    "        tmix_str='-'.join(tmix_list)\n",
    "        str7=str7.replace('温度',tmix_str)\n",
    "\n",
    "    x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year= read_xy(csvpath+'/%s'%str5)\n",
    "\n",
    "    #检查data数据导出时命名是否正确\n",
    "    check_value = check[0]\n",
    "    if check_value != int(str1):\n",
    "            print('value值和data数据内不一致')\n",
    "    all_value = data_process(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year,0.05)\n",
    "    \n",
    "\n",
    "    for index,j in enumerate(k_list):\n",
    "        if j[\"r\"]<0.1 or (str5.split('.')[1] in manual_vert_hori_dict and (str5_4 in manual_vert_hori_dict[str5.split('.')[1]] or not manual_vert_hori_dict[str5.split('.')[1]])):\n",
    "            is_hexline = input(\"【拟合很差】图%s第%s条线拟合很差，是否用水平或垂线替代？\\n水平替代输入1，垂线替代输2，不替代输入0，特殊情况(见代码头部)输其他\"%(str7,index))\n",
    "            if is_hexline == \"1\":\n",
    "                j[\"type\"] = \"horizon\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "            if is_hexline == \"2\":\n",
    "                j[\"type\"] = \"vertical\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "            if is_hexline == \"3\":\n",
    "                j[\"type\"] = \"horizon\"\n",
    "                k_list = [j]\n",
    "                ischange = True\n",
    "                break\n",
    "            if is_hexline == \"4\":\n",
    "                j[\"type\"] = \"vertical\"\n",
    "                k_list = [j]\n",
    "                ischange = True\n",
    "                break\n",
    "        elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) <2 and j[\"r\"]<0.3:\n",
    "            is_hexline = input(\"【拟合较差】图%s第%s条线可能近似水平线，是否用水平线替代？\\n确定输入1  不是输入0 用垂线替代输9 \"%(str7,index))\n",
    "            if is_hexline == \"1\":\n",
    "                j[\"type\"] = \"horizon\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "            if is_hexline == \"9\":\n",
    "                j[\"type\"] = \"vertical\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "        elif j[\"type\"] == \"normal\" and abs(j[\"k\"]) >30 and j[\"r\"]<0.3:\n",
    "            is_hexline = input(\"【拟合较差】图%s第%s条线可能近似垂直线，是否用垂直线替代？\\n确定输入1  不是输入0  用水平线替代输9\"%(str7,index))\n",
    "            if is_hexline == \"1\":\n",
    "                j[\"type\"] = \"vertical\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "            if is_hexline == \"9\":\n",
    "                j[\"type\"] = \"horizon\"\n",
    "                k_list[index] = j\n",
    "                ischange = True\n",
    "    k_dict[str5] = {\"k_list\" :k_list,\"writel\":{0 : (0,30) }}\n",
    "    writel = {0 : (0,30) }\n",
    "    overjump = False\n",
    "    if ischange:\n",
    "        fig, ax = plt.subplots(figsize = (17,13))\n",
    "\n",
    "        figure_drawing,regression2,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4 ,writel,k_list= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 2,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,**fig_opt)\n",
    "        overjump = False\n",
    "\n",
    "        for i in k_dict[str5][\"k_list\"]:\n",
    "            if i[\"type\"] == \"vertical\" and len(k_dict[str5][\"k_list\"]) == 2:\n",
    "                overjump = True\n",
    "        if not overjump:        \n",
    "            k_dict[str5][\"k_list\"] = k_list\n",
    "        k_dict[str5][\"writel\"] = writel\n",
    "        fig_save_address1=save_path.replace('\\\\','/')+'/有序图/%s.png'\n",
    "        plt.savefig(fig_save_address1%(str7.replace(\"/\",\"_\")),dpi = 300,bbox_inches='tight')\n",
    "        plt.clf()\n",
    "        plt.close('all')#清除当前画布，减少内存\n",
    "\n",
    "    if not overjump:        \n",
    "        figure_drawing2,regression22,x_12,y_12,x_22,y_22,x_32,y_23,x_24,y_24 ,writel,k_list2= re_figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 2,0.05,fig,ax,str5_4,str5_2_1,k_list,csvpath+'/%s'%str5,writel = writel,**fig_opt)\n",
    "\n",
    "    overjump = False\n",
    "    for i in k_dict[str5][\"k_list\"]:\n",
    "        if i[\"type\"] == \"vertical\" and len(k_dict[str5][\"k_list\"]) == 2:\n",
    "            overjump = True\n",
    "    if not overjump:        \n",
    "        k_dict[str5][\"writel\"] = writel\n",
    "    #总体拟合\n",
    "\n",
    "    fig, ax = plt.subplots(figsize = (17,13))\n",
    "\n",
    "    figure_drawing1,regression1,x_1,y_1,x_2,y_2,x_3,y_3,x_4,y_4,k_list= figure_drawing(x, y,check,p_pe,p1_pe1,p2_pe2 ,p3_pe3,p4_pe4,p5_pe5,p6_pe6,p7_pe7,p8_pe8,p9_pe9,p10_pe10,p11_pe11,p12_pe12,total,p,pe,dem,tp_year, 1,0.05,fig,ax,str5_4,str5_2_1, **fig_opt)    \n",
    "\n",
    "\n",
    "    plt.close('all')\n",
    "    if len(regression2)==1:\n",
    "        regression2.append(['/','/','/'])\n",
    "    csv_number+=all_value['n_check']\n",
    "    L=[str2,str1,str3,str4,total,all_value['n_check'],temprList[int(str5_2_1)].replace('温度',tmix_str),\n",
    "       str5_1[2:],str5_3,str5_4,\n",
    "       regression1[0][2].replace('$','').split('(')[0],regression1[0][0],regression1[0][1],judge_p(regression1[0][1]),\n",
    "       regression2[0][2].replace('$','').split('(')[0],regression2[0][0],regression2[0][1],judge_p(regression2[0][1]),\n",
    "       regression2[1][2].replace('$','').split('(')[0],regression2[1][0],regression2[1][1],judge_p(regression2[1][1]),\n",
    "      all_value['TB_max'],all_value['TB_min'],all_value['TB_mean'],all_value['TB_std'],\n",
    "      all_value['E_max'],all_value['E_min'],all_value['E_mean'],all_value['E_std'],\n",
    "      all_value['p_max'],all_value['p_min'],all_value['p_mean'],all_value['p_std'],\n",
    "      all_value['pe_max'],all_value['pe_min'],all_value['pe_mean'],all_value['pe_std'],\n",
    "      all_value['DEM_max'],all_value['DEM_min'],all_value['DEM_mean'],all_value['DEM_std'],\n",
    "      all_value['tp_year_max'],all_value['tp_year_min'],all_value['tp_year_mean'],all_value['tp_year_std']]\n",
    "    rows.append(L)\n",
    "if csv_number==total:\n",
    "    print('\\n各csv点数之和与该群系总点数相等')\n",
    "else:\n",
    "    print('\\n各csv点数之和与该群系总点数不相等,请检查:\\n1.该群系的所有csv是否同时跑了\\n2.是否有漏点情况')\n",
    "\n",
    "print('\\n开始生成信息统计csv\\n--------分割线--------')\n",
    "csv_save_address=save_path.replace('\\\\','/')+'/%s.csv'\n",
    "with open(csv_save_address%(str8.replace(\"/\",\"_\")),'w',newline='') as f1:\n",
    "    f1_csv = csv.writer(f1)\n",
    "    f1_csv.writerow(headers)\n",
    "    f1_csv.writerows(rows)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
